package com.modai.springcloud.service.impl;

import com.modai.springcloud.dao.OrderDao;
import com.modai.springcloud.entities.Order;
import com.modai.springcloud.feign.AccountService;
import com.modai.springcloud.feign.StorageService;
import com.modai.springcloud.service.OrderService;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * @author ：modai
 * @date ：Created in 2020-12-21 11:15
 * @description：
 */



@Service
@Slf4j
public class OrderServiceImpl implements OrderService {

    @Resource
    private OrderDao orderDao;

    @Resource
    private AccountService accountService;

    @Resource
    private StorageService storageService;
    /**
     * 创建订单->调用库存服务扣减库存->调用账户服务扣减账户余额->修改订单状态
     */
    @Override
//    @GlobalTransactional(name = "fsp_tx_group",rollbackFor = Exception.class)
    @GlobalTransactional
//    @Transactional
    public void create(Order order) {
        log.info("开始创建订单");
        //新建订单
        orderDao.create(order);
        //扣减库存
        log.info("开始调用库存微服务，扣减库存");
        storageService.decrease(order.getProductId(),order.getCount());
        log.info("库存end");

        //扣减金额
        log.info("开始调用账户微服务，扣减金额");
        accountService.decrease(order.getUserId(),order.getMoney());
        log.info("金额end");

        //更新订单状态
        log.info("订单完成");
        orderDao.update(order.getUserId(),0);
        log.info("状态更新完毕\n订单：{}结束",order.getProductId());
    }
}


